import 'package:bot_toast/bot_toast.dart';
import 'package:easy_read_comic/internal/api/comic_api.dart';
import 'package:easy_read_comic/models/page_model.dart';
import 'package:get/get.dart';

import '../../models/comic_model.dart';

class SearchListController extends GetxController {
  late String keyword;

  List<ComicModel> list = [];

  int currentPage = 0;

  bool loadDone = false;

  @override
  void onInit() {
    super.onInit();
    this.keyword = Get.arguments["keyword"].toString();
    this.queryComicList();
  }

  void queryComicList() {
    if (this.loadDone) {
      BotToast.showText(text: "已全部加载完成");
      return;
    }
    ComicApi()
        .list(title: this.keyword.toString(), currentPage: this.currentPage)
        .then((response) {
      if (response.data["code"] == 1) {
        for (var item in response.data["data"]["List"] ?? []) {
          this.list.add(ComicModel.formJson(item));
        }
        var page = PageModel.fromJson(response.data["data"]["Page"]);
        if (page.hasNextPage) {
          this.currentPage = page.NextPage;
        } else {
          this.loadDone = true;
        }
        this.update();
      }
    });
  }

  void resetQueryComicList() {
    this.loadDone = false;
    this.currentPage = 0;
    this.list.clear();
    this.queryComicList();
  }
}
